package com.chuiyuan;

import com.chuiyuan.dao.PoemDao;
import com.chuiyuan.dao.PoemDaoImpl;
import com.chuiyuan.dao.PoemDaoImplMyBatis;
import com.chuiyuan.db.ConnectionFactory;
import com.chuiyuan.entity.Poem;
import com.chuiyuan.utils.MyWorkQueue;
import com.chuiyuan.utils.ThreadPool;

import java.sql.SQLException;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Begin" );
        //new App().getGuShiWenSingleThread();
        //new App().getGuShiWenThreadPool();
        new App().testMyBatis();
    }

    public void testMyBatis(){
        PoemDao poemDao = new PoemDaoImplMyBatis(
                ConnectionFactory.getSqlSessionFactory());
        List<Poem> poemList =null ;
        try {
            poemList = poemDao.findAll();
        }catch (SQLException sqle){
            sqle.printStackTrace();
        }
        for (Poem poem: poemList){
            System.out.println(poem.toString());
        }
    }

    /**
     * single thread model,
     * save to mysql
     */
    public void getGuShiWenSingleThread(){
        String url ="http://so.gushiwen.org/gushi/tangshi.aspx/";

        HttpService httpService = new HttpService();

        ProcessDoc processDoc= new ProcessDoc();

        List<Poem> poemList = processDoc.processGuShiWen(url, httpService);


        //get poem content details
        for (Poem poem : poemList){
            processDoc.processDetails(poem,httpService);
            System.out.println(poem.toString());
        }

        //store to mysql
        PoemDao poemDao = new PoemDaoImpl() ;
        for (Poem poem: poemList){
            try {
                poemDao.add(poem);
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    /**
     *
     * thread pool
     */
    public void getGuShiWenThreadPool(){
        String url ="http://so.gushiwen.org/gushi/tangshi.aspx/";

        HttpService httpService = new HttpService();

        ProcessDoc processDoc= new ProcessDoc();

        List<Poem> poemList = processDoc.processGuShiWen(url, httpService);

        //get poem content details
        /*for (Poem poem : poemList){
            processDoc.processDetails(poem,httpService);
            System.out.println(poem.toString());
        }*/
        //long start = System.currentTimeMillis() ;
        MyWorkQueue wq = new MyWorkQueue(4);//5 thread
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        ThreadPool threadPool = ThreadPool.getThreadPool(10);
        //poemList.size() Runnable jobs
        for (Poem poem :poemList){
            DetailsTask task = new DetailsTask(poem,httpService,processDoc);
            wq.execute(task);
            //executorService.execute(task);
            //threadPool.execute(task);
        }

        System.out.println(Thread.activeCount());
        //executorService.shutdown();
        //threadPool.shutdown();
        System.out.println(Thread.activeCount());


        //store to mysql
       /* PoemDao poemDao = new PoemDaoImpl() ;
        for (Poem poem: poemList){
            try {
                poemDao.add(poem);
            }catch (SQLException e){
                e.printStackTrace();
            }
        }*/
        Executor es = null;
        Executors executors = null ;
        ArrayDeque arrayDeque = null ;
        Queue q = null ;
        ExecutorService service = null ;
        Callable callable =  null ;
        //ThreadLocal
    }
}
